const mongoConnected = require("../lib/mongoose_connect");
const Mongoose = require("mongoose");
const mongoModels = require("../lib/mongoose_schemas");
const lodash = require("lodash");
const fs = require("fs-extra");
const path = require("path");
const SAFEZONE_GRID_DATA = require("../test/210216_grids_in_safezone.json");
let GRID_WIDTH = SAFEZONE_GRID_DATA.GRID_LENGTH;
// GRID_WIDTH = 2;
/**
 * 双线性插值
 * @param {{x:Number,y:Number}} p 
 */
function safetyValueOfPoint(p) {
  let half = GRID_WIDTH / 2;
  let xi = parseInt(p.x / half);
  if (xi % 2 === 0) {
    xi--;
  }
  let yi = parseInt(p.y / half);
  if (yi % 2 === 0) {
    yi--;
  }
  //{xi,yi}就是P左上角的位置点了
  let getval = (x, y) => {
    try {
      let v = SAFEZONE_GRID_DATA.GRIDS_IN_SMALL[x][y] || 0;
      // debugger
      return v;
    } catch (e) {
      return 0;
    }
  }
  let Q12 = {
    x: xi * half,
    y: yi * half, val: 0
  };
  let Q22 = {
    x: xi * half + 2 * half,
    y: yi * half, val: 0
  };
  let Q11 = {
    x: xi * half,
    y: yi * half + half * 2, val: 0
  }
  let Q21 = {
    x: xi * half + half * 2,
    y: yi * half + half * 2, val: 0
  }
  let arr = [Q11, Q12, Q21, Q22];
  arr.forEach(e => {
    let xi = (e.x - half) / (half * 2);
    let yi = (e.y - half) / (half * 2);
    let val = getval(xi, yi);
    e['val'] = val;
    // debugger
  });
  let valR1 = (Q21.x - p.x) / (Q21.x - Q11.x) * Q11.val + (p.x - Q11.x) / (Q21.x - Q11.x) * Q21.val;
  let valR2 = (Q22.x - p.x) / (Q22.x - Q12.x) * Q12.val + (p.x - Q12.x) / (Q22.x - Q12.x) * Q22.val;
  let valP = (p.y - Q12.y) / (Q11.y - Q12.y) * valR1 + (Q11.y - p.y) / (Q11.y - Q12.y) * valR2


  // debugger
  return valP;

}

mongoConnected.then(async $ => {
  let q = (await mongoModels.TB21_luodian.find({
    "map_name": "Savage_Main"
  }, {
    "luodian": "$luodian",
    "safetyzone_value": "$safetyzone_value",
    "_id": 0
  })).map(e => e._doc);
  await fs.writeFile(path.join(__dirname, "../test/210217_验证我的safetyzonevalue.json"), JSON.stringify(q))
  debugger
  return
  let setMany = await mongoModels.TB21_luodian.updateMany({
    "map_name": {
      "$ne": "Savage_Main"
    }
  }, {
    $set: {
      "safetyzone_value": null
    }
  });
  debugger

  return
  while (true) {
    // mongoModels.TB21_luodian.bu
    let found = await mongoModels.TB21_luodian.find({
      "safetyzone_value": null,
      map_name: "Savage_Main"
    }).limit(1000);
    console.log(found[0].id, found[0].account_id, found[0].match_id);
    // debugger
    if (found.length == 0) {
      debugger
      break
    }
    found.forEach(e => {
      // e.safetyzone_value = 
      // debugger
    });
    let bulkArrOps = found.map(e => {
      let safetyzone_value = safetyValueOfPoint(e.luodian);
      // debugger
      return {
        "updateOne": {
          filter: {
            _id: e._id
          },
          update: {
            $set: {
              safetyzone_value: safetyzone_value
            }
          }
        }
      }
    })
    let updated = await mongoModels.TB21_luodian.bulkWrite(bulkArrOps)
    // debugger
  }
  debugger
})